package com.hotkid.ceo.jetpack.myjetpackmvvm.data.http

import android.util.Log
import com.hotkid.ceo.jetpack.myjetpackmvvm.data.http.interceptor.AddCookiesInterceptor
import com.hotkid.ceo.jetpack.myjetpackmvvm.data.http.interceptor.ReceivedCookiesInterceptor
import okhttp3.OkHttp
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

/**
 * ClassName:RetrofitClient
 * Package:com.hotkid.ceo.jetpack.myjetpackmvvm.data.http
 * Description:
 * @date:2021/3/19 4:07 PM
 * @author:774169396@qq.com
 */
object RetrofitClient {
    private const val CALL_TIMEOUT = 10L
    private const val CONNECT_TIMEOUT = 20L
    private const val IO_TIMEOUT = 20L
    private var apiService: Api

    fun getApiService(): Api {
        return apiService
    }


    init {
        val loggingInterceptor = HttpLoggingInterceptor { Log.d("httpLog", it) }
        loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY

        /**
         * OKhttpCilent
         */

        val okHttpClient = OkHttpClient.Builder()
                .callTimeout(CALL_TIMEOUT, TimeUnit.SECONDS)
                .readTimeout(IO_TIMEOUT, TimeUnit.SECONDS)
                .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
                .writeTimeout(IO_TIMEOUT, TimeUnit.SECONDS)
                .addInterceptor(AddCookiesInterceptor())
                .addInterceptor(ReceivedCookiesInterceptor())
                .addInterceptor(loggingInterceptor)
                .retryOnConnectionFailure(true)
                .build()
        /**
         * Refrofit
         */

        val  retrofit= Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl(Api.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build()


        apiService=retrofit.create(Api::class.java)
    }

}